package com.hr.垃圾回收;

/**
 * @program: leetcode
 * @description: 测试可复活对象
 * @author: HuRan QQ1345122890
 * @create: 2021-03-14 10:54
 */
public class CanReliveObj {
    public  static  CanReliveObj obj;

    //会被执行一次
    @Override
    protected void finalize() throws Throwable {
        super.finalize();
        System.out.println("调用当前类重写该方法");
        obj=this;
    }

    public static void main(String[] args) throws InterruptedException {
        obj=new CanReliveObj();
        obj=null;
        System.gc();
        Thread.sleep(2000);
        if(obj==null){
            System.out.println("obj 被回收");
        }else {
            System.out.println("obj还活着");
        }
        obj=null;
        System.gc();
        Thread.sleep(2000);
        if(obj==null){
            System.out.println("obj 被回收");
        }else {
            System.out.println("obj还活着");
        }
    }
}